<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- tree组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doEdit(scope.data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doDel(scope.data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹窗 -->
        <!-- <el-dialog title="编辑 | 添加 部门" :visible.sync="showDialog" v-if="showDialog">
          <departmentDialog :pid="curId" :isEdit="isEdit" @success="doSuccess"></departmentDialog>
        </el-dialog> -->
        <el-dialog :title="(isEdit ? '编辑' : '添加') + '部门'" :visible.sync="showDialog" @close="doCancel">

          <departmentDialog ref="dialogRef" :pid="curId" :is-edit="isEdit" :origin-list="originList" @success="doSuccess" @hclose="doClose" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteDepartment } from '@/api/departments'
import { arrayToTree } from '@/utils/arrayToTree'
import departmentDialog from '@/views/departments/departmentDialog.vue'
export default {
  components: { departmentDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 是否展示弹窗
      showDialog: false,
      // 增加部门的id
      curId: '',
      // 判断是否是编辑页面
      isEdit: false,
      // 父传子校验部门信息
      originList: []
    }
  },
  created() {
    // 加载部门信息
    this.loadDepartments()
  },
  methods: {
    // 1.加载部门信息
    async loadDepartments() {
      const { data: res } = await getDepartments()
      // console.log('部门信息',res.data.depts);
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return { id: item.id, name: item.name, code: item.code, pid: item.pid }
      })
      this.list = arrayToTree(res.data.depts, '')
      // console.log(this.list);
    },
    // 2.新增部门弹出弹框
    doAdd(id) {
      console.log(id)
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 3.新增部门成功,关闭页面,刷新列表
    doSuccess() {
      this.showDialog = false
      // 刷新列表
      this.loadDepartments()
    },
    // 4.删除部门
    doDel(id) {
      this.$confirm('是否退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data: res } = await deleteDepartment(id)
        // console.log(res);
        if (res.code === 10000) {
          this.$message.success(res.message)
          // 刷新列表
          this.loadDepartments()
        }
      }).catch(() => {
      })
    },
    // 编辑部门
    doEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        // console.log( 'refs信息',this.$refs.dialogRef);
        this.$refs.dialogRef.loadDepartmentById()// 调用子组件
      })
    },
    // 在子组件点击取消,关闭弹窗
    doClose() {
      this.showDialog = false
    },
    // 关闭回调
    doCancel() {
      this.$refs.dialogRef.restForm()
    }

  }
}
</script>

<style>

</style>
